package com.example.go_drink_water_plugin;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.application.ApplicationManager;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author wanghuafu
 * @version 0.1
 * @date 2023/11/30
 */
public class ReminderTask extends TimerTask {

    private static Timer reminderTimer;

    public static void startReminder(int intervalMinutes) {
        if (reminderTimer != null) {
            reminderTimer.cancel();
        }
        reminderTimer = new Timer();
        long interval = intervalMinutes * 60 * 1000;
        // 将间隔转换为毫秒并启动定时器
        reminderTimer.schedule(new ReminderTask(), interval,interval);
    }

    public static void close() {
        if (reminderTimer != null) {
            reminderTimer.cancel();
        };
    }

    @Override
    public void run() {
        ApplicationManager.getApplication().invokeLater(()->{
            Notification notification = new Notification(
                    "PluginGroupId",
                    "喝水提醒",
                    TipConstant.getNextTip(),
                    NotificationType.INFORMATION
            );
            Notifications.Bus.notify(notification);
        });
    }
}
